# coding: utf-8
from collections import defaultdict
import sys,json
sys.path.append('/home/zhengzhiqiang/workflow')
#from tools import ddRobot
from pathlib import Path
import requests


def readlength(indir):
#/mnt/share/S200036326/L01/version.json    
    fcid=Path(indir).name
    version=f"/mnt/share/{fcid}/L01/version.json"
    with open(version,'r')as f1:
        fo=f1.read()
        fp=json.loads(fo)['Read1Length']
        rl=int(fp)-1
    return rl


def dealSampleSheet(indir):
    runid = indir.rstrip('/').split('/')[-1]
    with open(indir.rstrip('/')+f'/fastq/{runid}.fastq.Stats.xls') as infile:
        infile.readline()
        d = {}
        for i in infile:
            if i.strip():
                name, index, reads = i.split()[:3]
                reads = ''.join(reads.split(','))
                d[name] = index + '@' + reads
            else:
                break
        return d

def dealInfo(infile):
    with open(infile) as inf:
        d = {}
        for i in inf:
            x = i.strip().split()
            d[x[0]] = x[1] + '@' + x[2]
        return d

def dealQ30(indir):
    with open(indir.rstrip('/')+'/fastq/q30.stat.xls') as infile:
        d = {}
        for i in infile:
            x = i.strip().split()
            d[x[0]] = str(x[1])
        return d

def dealSex(indir):
    with open(indir+'/all.sex.xls') as infile:
        infile.readline()
        d = {}
        for i in infile:
            x = i.strip().split()
            if x[-1] == 'male':
                x[-1] = '男'
            else:
                x[-1] = '女'
            d[x[0]] = x[1:]
        return d

def dealSample(indir, sample):
    with open(indir + f'/{sample}/{sample}.unmap.fastq') as rh, open(indir + f'/{sample}/{sample}.species.xls') as kk,  open(indir + f'/{sample}/{sample}.remained.fasta') as unjt, \
        open(indir + f'/{sample}/{sample}.removepla.fasta') as unzl, open(indir + f'/{sample}/{sample}.removedupKK.fasta') as undup, open(indir + f'/{sample}/{sample}.kraken2.fasta') as krak:
        num = 0
        for i in rh:
            num += 1
        num /= 4

        unjt_num = 0
        for i in unjt:
            unjt_num += 1
        unjt_num /= 2

        unzl_num = 0
        for i in unzl:
            unzl_num += 1
        unzl_num /= 2
        
        undup_num = 0
        for i in undup:
            undup_num += 1
        undup_num /= 2

        krak_num = 0
        for i in krak:
            krak_num += 1
        krak_num /= 2

        dsum = 0
        try:
            with open(indir + f'/{sample}/{sample}.dupstat') as dup:
                for i in dup:
                    dsum += int(i.split()[0]) - 1
        except:
            dsum = 0
        
#        nb_reads=0        
#        try:        
#            with open(indir + f'/{sample}/{sample}_neibiao.xls') as nbf:
#
#                for line in nbf:
#                    if line.startswith('all_DNA'):
#                        nb_reads+=int(line.strip().split('\t')[1])
#                    elif line.startswith('all_CF'):
#                        nb_reads+=int(line.strip().split('\t')[1])
#        except:
#            nb_reads=0
        
        header = kk.readline().split()
        counti = 0
        ranki = 0
        si = 0
        for i, e in enumerate(header):
            if e.startswith('Count'):
                counti = i
            elif e == 'D':
                ranki = i
            elif e == 'S':
                si = i
        RhizobiumReads = 0
        BurkholderiaReads = 0
        rankd = defaultdict(lambda: defaultdict(int))
        for line in kk:
            x = line.split()
            if x[si] == '-':
                continue
            if x[si].find('Rhizobium_leguminosarum') > -1:
                RhizobiumReads = int(x[counti])
            if x[si].find('_Burkholderia') > -1:
                BurkholderiaReads += int(x[counti])
            rankd[x[ranki]][x[si]] += int(x[counti])
        return num, unjt_num, unzl_num, undup_num, krak_num, dsum, [rankd, RhizobiumReads, BurkholderiaReads]

def main(indir, infile):
    info = dealInfo(infile)
    ss = dealSampleSheet(indir)
    sex = dealSex(indir)
    q30 = dealQ30(indir)
    rl = int(readlength(indir))
    with open(indir+'/runStat.xls', 'w', encoding='gbk') as out:
    #with open('./runStat.xls', 'w', encoding='gbk') as out:
        out.write('''样本\t索引\t样本类型\t常染色体平均覆盖\t常染色体中位覆盖\tX染色体覆盖\tY染色体覆盖\t\
程序判定\t表格信息\t拆分reads数\t人源reads数\t人源比例\t非人源reads数\t非人源比例\t非人源重复reads数\t非人源重复比例\tQ30\t微生物总数\t\
接头reads数\t接头比例\t质粒reads数\t质粒比例\tunclassify\tunclassify比例\t总数据量\n''')
        for k, v in ss.items():
            Dc = {'D_10239_Viruses':0, 'D_2_Bacteria':0, 'D_2759_Eukaryota':0}
            Ds = {'D_10239_Viruses':0, 'D_2_Bacteria':0, 'D_2759_Eukaryota':0}
            result = []
            if k == 'Undetermined' or k.find('undecoded') > -1 or k.find('otherindex') > -1 or k.find('TotalReads') > -1:
                continue
            result.append(k)
            index, reads = v.split('@')
            reads = int(reads)
            result.append(index)
            s, t = [v2 for k2, v2 in info.items() if k.startswith(k2)][0].split('@')
            result.append(t)
            result.extend(sex[k])
            result.append(s)
            result.append(f'{reads:,}')
            left, unjtn, unzln, undupn, krakn, dupreads, rankinfo  = dealSample(indir, k)
            human = reads - left
#            left=left0+nb_reads
            result.append(f'{int(human):,}')
            if reads == 0:
                humanR = '0'
            else:
                humanR = f'{human / reads:.2%}'
            result.append(humanR)
            result.append(f'{int(left):,}')
            if reads == 0:
                leftR = '0'
            else:
                leftR = f'{left / reads:.2%}'
            result.append(leftR)
            result.append(f'{dupreads:,}')
            if left == 0:
                dupreadsR = f'{dupreads / 1000:.2%}'
            else:
                dupreadsR = f'{dupreads / left:.2%}'
            result.append(dupreadsR) 
            if k in q30:
                result.append(q30[k])
            else:
                result.append('0')
#            result.append(f'{rankinfo[1]:,}')
            rank = rankinfo[0]
            microbeSum = 0
            for dk in Dc:
                for rk, rv in rank[dk].items():
                    microbeSum += rv
                    Dc[dk] += rv
                    Ds[dk] += 1
#            result.extend([f'{c:,}' for c in Dc.values()])
            result.append(f'{microbeSum:,}')
            if left == 0:
                microbeR = f'{microbeSum / 1000:.2%}'
            else:
                microbeR = f'{microbeSum / left:.2%}'
#            result.append(microbeR)
#            result.extend([f'{s:,}' for s in Ds.values()])
#            result.append(f'{sum(Ds.values()):,}')
#            result.append(f'{rankinfo[2]:,}')
            if microbeSum == 0:
                BurkholderiaR = f'{rankinfo[2] / 1000:.2%}'
            else:
                BurkholderiaR = f'{rankinfo[2] / microbeSum:.2%}'
#            result.append(BurkholderiaR)

            result.append(f'{int(left-unjtn):,}')
            if left == 0:
                jtR = '-'
            else:
                jtR = f'{(left-unjtn) / left:.2%}'
            result.append(jtR)
            result.append(f'{int(unjtn-unzln):,}')
            if left == 0:
                zlR = '-'
            else:
                zlR = f'{(unjtn-unzln) / left:.2%}'
            result.append(zlR)
            result.append(f'{int(undupn-krakn):,}')
            if left == 0:
                unclaR = '-'
            else:
                unclaR = f'{(undupn-krakn) / left:.2%}'
            result.append(unclaR)  
            result.append(str(rl*reads))
            

            out.write('\t'.join(result)+'\n')

if __name__ == '__main__':
    main(sys.argv[1], sys.argv[2])
    url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=90434457-2bc2-45f4-8c5d-4c781ec72314"
    data = {"msgtype": "text", "text": {"content": "广州本地流程结束",}}
    print(data['text']['content'])
    # r = requests.post(url, json=data)
    url2 = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5a6bc9cf-6fac-4d03-bf7c-d722263b1cc8"
    data2 = {"msgtype": "text", "text": {"content": "广州服务器已使用完毕！","mentioned_list":["zhaoyijia"],"mentioned_mobile_list":["15104602330"]}}
    #t = requests.post(url2, json=data2)
